package com.biglead.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.biglead.admin.api.CommonPage;
import com.biglead.admin.api.CommonResult;
import com.biglead.admin.param.UmsAdminParam;
import com.biglead.admin.pojo.UmsMenu;
import com.biglead.admin.pojo.UmsRole;
import com.biglead.admin.service.UmsAdminService;
import com.biglead.admin.service.UmsRoleService;
import com.biglead.common.pojo.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 后台用户管理Controller
 * Created by macro on 2018/4/26.
 */
@Controller
@Api(tags = "UmsAdminController")
@RequestMapping("/admin/user")
public class AdminUserController {

    @Autowired
    private UmsAdminService adminService;
    @Autowired
    private UmsRoleService roleService;
    @ApiOperation(value = "用户注册")
    @PostMapping(value = "/register")
    @ResponseBody
    public CommonResult<UserInfo> register(@RequestBody UmsAdminParam umsAdminParam) {
        UserInfo umsAdmin = adminService.register(umsAdminParam);
        if (umsAdmin == null) {
            return CommonResult.failed();
        }
        return CommonResult.success(umsAdmin);
    }
    @GetMapping(value = "/info")
    @ResponseBody
    public CommonResult getAdminInfo(@RequestHeader Long userId) {

        //获取用户的详情
        UserInfo userInfo = adminService.queryById(userId);
        //查询用户的授权菜单
        List<UmsMenu> menuList = roleService.getMenuList(userInfo.getId());
        //查询用户的角色
        List<UmsRole> roleList = adminService.getRoleList(userInfo.getId());

        Map<String, Object> data = new HashMap<>();
        data.put("username", userInfo.getUsername());
        data.put("menus", menuList);
        data.put("icon", userInfo.getIcon());
        if(roleList.size()>0){
            List<String> roles = roleList.stream().map(UmsRole::getName).collect(Collectors.toList());
            data.put("roles",roles);
        }
        return CommonResult.success(data);
    }

    /**
     * 根据用户名或姓名分页获取用户列表
     * @param keyword
     * @param pageSize
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Page<UserInfo>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<UserInfo> adminList = adminService.pageList(keyword, pageSize, pageNum);
        return CommonResult.success(adminList);
    }

    /**
     * 修改指定用户信息
     * @param id
     * @param admin
     * @return
     */
    @PostMapping(value = "/update/{id}")
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody UserInfo admin) {
        boolean count = adminService.updateUserinfo(id, admin);
        if (count) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除指定用户信息")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        boolean count = adminService.deleteFromId(id);
        if (count) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改帐号状态")
    @PostMapping(value = "/updateStatus/{id}")
    @ResponseBody
    public CommonResult updateStatus(@PathVariable Long id,@RequestParam(value = "status") Integer status) {
        boolean count = adminService.updateStatusFromId(id,status);
        if (count) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }


    @ApiOperation("给用户分配角色")
    @RequestMapping(value = "/role/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateRole(@RequestParam("adminId") Long adminId,
                                   @RequestParam("roleIds") List<Long> roleIds) {
        boolean count = adminService.updateUserRole(adminId, roleIds);
        if (count) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取指定用户的角色")
    @RequestMapping(value = "/role/{adminId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<UmsRole>> getRoleList(@PathVariable Long adminId) {
        List<UmsRole> roleList = adminService.getRoleList(adminId);
        return CommonResult.success(roleList);
    }
}
